/**
 * Created by Amaris on 2016/12/6.
 */
var async = require('async');
var http=require('http');
var express = require('express');
var router = express.Router();
var stockRecommendCtrl = require('../../controller/controller.stockrecommend.js');


router.post('/createStockRecommend', function (req, res, next) {
    stockRecommendCtrl.create(req.body, function (err, result) {
        if (err) throw err;
        res.send(result);
    });

});



router.get('/getStockRecommendListByUid', function (req, res, next) {
    stockRecommendCtrl.getListByCreateUid(req.query, function (err, result) {
        if (err) throw err;
        res.send(result);
    });
});

router.get('/getStockRecommendList', function (req, res, next) {
        var params = req.query;
        if (params.queryStr == null) {
            params.queryStr = "";
        }
        params.actionType = "stockrecommend";
        params.isRecommend = '1';
        params.enable = '1';
        var self = this;
        var mconferences = {};
        var stockParam = "";
        async.series({
            mconferencelist: function(callback){
                stockRecommendCtrl.getListByActionType(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        mconferences = result.obj;
                        callback(null, result.obj);
                    }
                })
            },
            stockList: function(callback){
                for(i in mconferences){
                    var stockCode = mconferences[i].stockId==null?"":mconferences[i].stockId.stockCode;
                    var stocks =stockCode.split(".");
                    stockCode = stocks[1].toLowerCase()+stocks[0];
                    stockParam += stockCode+",";
                }
                callback(null,stockParam);
            },
            stockVals:function(callback){
                var url = 'http://hq.sinajs.cn/list='+stockParam;
                http.get(url,function(req){
                    var html='';
                    req.on('data',function(data){
                        html+=data;
                    });
                    req.on('end',function(){
                        var els =html.split(";");
                        var stoks = [];
                        for(i in els){
                            var elements = els[i].split(",");
                            if(elements.length>=4){
                                stoks.push(elements[3]);
                            }else{
                                stoks.push(-1);
                            }
                        }
                        console.log(stoks)
                        callback(null,stoks);
                    });
                });

            }
        },function(err, results) {
                res.send(results)
        });






    // stockRecommendCtrl.getListByActionType(req.query, function (err, result) {
    //     if (err) throw err;
    //     res.send(result);
    // });
});
module.exports = router;
